<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" import="java.sql.*"%>
<%@ page import="ookweb.Conn"%>
<%@ page import="ookweb.Data"%>
<%@ page import="java.io.BufferedWriter"%>
<%@ page import="java.io.IOException"%>
<%@ page import="java.io.StringReader"%>
<%
 response.setContentType("text/html;charset=UTF-8");
 response.setHeader("Cache-Control","no-cache"); 
 %>
<%@ include file="../public.jsp" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/ookweb/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>">    
    <title>信息管理</title>    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/images/m/css.css">
	<script src="<%=basePath%>js/jquery-1.5.1.min.js"  type="text/javascript" ></script>
	<script src="<%=basePath%>js/public.js"  type="text/javascript" ></script>
  </head>
  
  <body><br/>
  <%   Data data=new Data(dbuser,dbpass,dburl);
     Conn con=new Conn(dbuser,dbpass,dburl);
     String iid=(String)request.getParameter("iid");
    String act=(String)request.getParameter("act"); 
    String cid=(String)request.getParameter("cid");
     String content="",title="",info="",keyword="",classid="";
    if("".equals(cid)||cid==null){cid="0";}
   
     ResultSet rssc=con.getRs("SELECT * FROM web_infoclass where sid="+cid);
     String cidarr=cid;
    try{  while(rssc.next()){
       cidarr=cidarr+","+rssc.getString("CLASSID");
     }}catch(Exception e){}
    //分页段 
     //变量声明 
String mysql=new String(); //SQL语句
mysql=" web_info where classid in("+cidarr+")";
int intRowCount=0;  //总的记录数
int intPageCount=0; //总的页数
int intPageSize=15; //每页显示的记录数
int intPage; //待显示页码 
String strPage=new String(); //用来接收当页码参数
int begin_no=0; //开始的rownum记录号
int end_no=0;  //结束的rownum记录号

//取得待显示页码 
strPage = request.getParameter("page"); 
if(strPage==null){//表明在QueryString中没有page这一个参数，此时显示第一页数据 
intPage = 1; 
} 
else{//将字符串转换成整型 
intPage = java.lang.Integer.parseInt(strPage); 
if(intPage<1) intPage = 1; 
} 
//得到总的数据记录行数
intRowCount=data.getRowCount(mysql); //总记录数

//计算总共要分多少页
intPageCount = (intRowCount+intPageSize-1) / intPageSize; 
//调整待显示的页码 

if(intPage>intPageCount) intPage = intPageCount; 
//分页段
     
     
     
     
    
    int intT=0;
    if("d".equals(act) && !("".equals(iid)) && iid!=null){
       data.delete("delete from web_info where iid="+iid);
       act=""; 
     }
    if(!("".equals(act) ||  act==null)){
      
      if("save".equals(act)){
          String sql="";
          content=(String)request.getParameter("content");
         
          title=(String)request.getParameter("title");
          info=(String)request.getParameter("info");
          keyword=(String)request.getParameter("keyword");
          classid=(String)request.getParameter("classid");
          if("".equals(classid)||classid==null){classid=cid;}
         int num=0;
         
         if(!("".equals(iid)) && iid!=null){
	         sql="update web_info set title='"+title+"',keyword='"+keyword+"',classid="+classid+",info='"+info+"',content=EMPTY_CLOB() where iid="+iid;
	         
	         String sqls="select content from web_info where iid="+iid+" for update"; 
	         
	         num=con.update2(sql,sqls,content);
	         if(num>0){out.print("<script>alert(\"已成功修改\");</script>");}
         }else{
         java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat("yyyy-MM-dd");
         java.util.Date currentTime=new java.util.Date();
         String time=formatter.format(currentTime);
         sql="select max(iid) as maxid from web_info";
         ResultSet rst=con.getRs(sql);
         int maxid=1000;
         if(rst.next()){
         maxid=rst.getInt("maxid");
         maxid=maxid+1;
         }
         sql="insert into web_info(iid,classid,title,keyword,info,content,addtime)values("+maxid+","+classid+",'"+title+"','"+keyword+"','"+info+"',EMPTY_CLOB(),to_date('"+time+"','yyyy/mm/dd'))";
         String sqls="select content from web_info where iid="+maxid+" for update";        
         num=con.insert2(sql,sqls,content);         
         if(num>0){out.print("<script>alert(\"已成功添加\");</script>");}
          act="";
         }
         response.sendRedirect(basePath+"webadmin/info/info.jsp?cid="+cid+"&page"+intPage);
      }else if("e".equals(act) && !("".equals(iid)) && iid!=null){	       
			 ResultSet rs=con.getRs("SELECT iid,title,keyword,info,classid,content FROM web_info where iid="+iid);
			 if(rs.next()){
			 title=rs.getString("title");iid=rs.getString("iid");
			 keyword=rs.getString("keyword");info=rs.getString("info");content="";
			 classid=rs.getString("classid");			
			 oracle.sql.CLOB clob=(oracle.sql.CLOB)rs.getClob("content");
			 content= Str.parse(clob);
			}
      }else{iid="";}
      %>
      <script src="<%=basePath%>ckeditor/ckeditor.js"  type="text/javascript" ></script>
      <script type="text/javascript" src="<%=basePath%>ckfinder/ckfinder.js"></script>
      
      <form action="<%=basePath%>webadmin/info/info.jsp?act=save&cid=<%=cid %>&iid=<%=iid %>&page=<%=intPage %>" method="post" id="frm">
	  <table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="92acbb">
	  <%
	 // out.print("select classid,classname from web_infoclass where sid="+cid);
 ResultSet rss=con.getRs("select classid,classname from web_infoclass where sid="+cid);
  rss.last();
 int rss_num=rss.getRow();
 if(rss_num>0){ %>
	  <tr>
	    <td align="center" width="140">分类</td>
	    <td align="left">
<select name="classid" id="classid">
<% 
for(int i=1;i<=rss_num;i++){
       rss.absolute(i);
 %>
  <option value="<%=rss.getString("classid") %>" <%if(classid.equals(rss.getString("classid"))){out.print("selected");} %>><%=rss.getString("classname") %></option>
<%} %>
</select>
</td>
	  </tr>
	  <%}%>
	   <tr>
	    <td align="center" width="140">标题</td>
	    <td align="left"><input name="title" id="title" style="width:400px;" type="text" class="inp" value="<%=title %>"/></td>
	  </tr>
	   <tr> 
	    <td align="center" width="140">关键字</td>
	    <td align="left"><input name="keyword" id="keyword" style="width:400px;" type="text" class="inp" value="<%=keyword %>"/></td>
	  </tr>
	  <tr>
	    <td align="center" width="140">简介</td>
	    <td align="left"><textarea name="info" style="height:60px;width:400px;"><%=info %></textarea></td>
	  </tr>
	  <tr>
	    <td align="center" width="140">内容</td>
	    <td align="left"><textarea name="content" id="content"><%=content %></textarea>
	    <script type="text/javascript">
			        var editor=CKEDITOR.replace('content');
			         CKFinder.SetupCKEditor(editor,'<%=basePath%>editor/ckfinder/'); 
			  </script>
	    
	    </td>
	  </tr>
	   <tr>
	    <td align="center" width="140">操作</td>
	    <td align="left"> <input name="cmd" id="savecmd" type="button" class="inp" value="保存" style="width:60px;"/> <input name="cmd" id="cmdreturn" type="button" class="inp" value="返回" style="width:60px;"/></td>
	  </tr>
	 </table>
	 </form>
   <script type="text/javascript">

   
	$(document).ready(function() {
		$('#savecmd').click(function() {
		  if($('#title').attr('value')==''){
		  alert('请输入标题');return;
		  }else{		
		    $("#frm").submit();
		  }
		});
		$('#cmdreturn').click(function() {
		window.history.back();
		});
	});
   </script>
   <% }else{%>
  【<span class="bt">信息管理</span>】
 《<a href="<%=basePath%>webadmin/info/info.jsp?act=a&cid=<%=cid %>" title="增加新信息" rel="gb_page_center[600,200]">添加新信息</a>》
 <table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="92acbb">
  <tr>
    <th  align="left">分类</th>
    <th width="80">标题</th>
    <th width="140">时间</th>
    <th width="140">操作</th>
  </tr>
  <% 

	    begin_no=(intPage-1) * intPageSize + 1; 
        end_no = intPage * intPageSize;
	    mysql="select a.*,b.classname from (select rownum row_id ,iid,classid,title,addtime from (select iid,classid,title,addtime from web_info where classid in("+cidarr+") order by iid desc)) a";
	    mysql=mysql+" left join web_infoclass b on a.classid=b.classid where row_id between " +begin_no+ " and " +end_no;
	   //out.print(mysql);
	    ResultSet rs=con.getRs(mysql);
		int i=0;
		
		String siid="";
		while(rs.next()){
		i++;title=rs.getString("title");
		siid=rs.getString("iid");
	 %>
	<tr onmouseover='$(this).css({"background":"#cee1ed"});' onmouseout='$(this).css({"background":"#fff"});'>
    <td  width="140"><% if("0".equals(rs.getString("classid"))){ out.print("顶级分类");}else{out.print(rs.getString("classname"));}%></td>
    <td  align="center"><%=title %></td>
    <td width="140" align="center"><%=rs.getDate("addtime") %></td>
    <td width="140" align="center">
    <span class="area"><a title="<%=title %>_修改"  href="<%=basePath%>webadmin/info/info.jsp?act=e&page=<%=intPage %>&cid=<%=cid %>&iid=<%=siid%>"> 编辑 </a></span> 
    <span class="area"><a href="javascript:confirmDelete('<%=basePath%>webadmin/info/info.jsp?act=d&iid=<%=siid%>&cid=<%=cid %>&page=<%=intPage %>');"> 删除 </a></span></td>
    </tr>
   <%} 
   if(intPageCount>1){
   %>
   <tr>
    <td colspan="5" align="center">
     第<%=intPage%>页 共<%=intPageCount%>页 
<a href="<%=basePath%>webadmin/info/info.jsp?page=1&cid=<%=cid %>">首页</a>
<%if(intPage>1){%><a href="<%=basePath%>webadmin/info/info.jsp?page=<%=intPage-1%>&cid=<%=cid %>">上一页</a><%}%> 
<%if(intPage<intPageCount){%><a href="<%=basePath%>webadmin/info/info.jsp?page=<%=intPage+1%>&cid=<%=cid %>">下一页</a><%}%> 
<a href="<%=basePath%>webadmin/info/info.jsp?page=<%=intPageCount%>&cid=<%=cid %>">尾页</a>
   </td>
   </tr>
   <%} %>
 </table>
 <% } con.close();%>
  </body>
</html>
